package com.kx.center.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.kx.center.entity.Goods;
import com.kx.center.mappers.GoodsMapper;
import com.kx.center.service.GoodsService;
import com.kx.center.util.DateUtil;
import com.kx.center.util.JSONParser;

@Service
public class GoodsServiceImpl implements GoodsService {
	@Autowired
	GoodsMapper goodsMapper;

	@Override
	public int insertGoods(Goods goods) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int updateGoods(Goods goods) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public List<Goods> findGoods(Integer storeId) {
		return goodsMapper.findGoodsByStoreId(storeId);
	}

	@Override
	public List<Goods> findGoodsByStoreIdAndTypeId(Integer storeId, Integer typeId) {
		return goodsMapper.findGoodsByStoreIdAndTypeId(storeId, typeId);
	}

	@Override
	public Goods findGoodsById(Integer id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<HashMap<String, Object>> findAllGoodsType() {
		return goodsMapper.findAllGoodsType();
	}

	@SuppressWarnings("unchecked")
	@Override
	@Transactional(propagation = Propagation.REQUIRES_NEW)
	public String getJsonGoods(Integer storeId, boolean flag) throws JsonProcessingException {

		try {
			List<Goods> goods_list = findGoods(storeId);
			List<HashMap<String, Object>> types = findAllGoodsType();
			List<HashMap<String, Object>> yestodayOrderedQuantity = findYestodayOrderedQuantityById(storeId);
			List<HashMap<String, Object>> yestodayselledQuantity = findYestodaySelledQuantityById(storeId);

			HashMap<Integer, HashMap<String, Object>> mapping = new HashMap<Integer, HashMap<String, Object>>();

			for (int i = 0; i < types.size(); i++) {
				HashMap<String, Object> type = types.get(i);
				type.put("data", new ArrayList<HashMap<String, Object>>());
				mapping.put((Integer) type.get("id"), type);
			}

			for (Goods goods : goods_list) {
				HashMap<String, Object> type = mapping.get(goods.getTypeId());
				ArrayList<HashMap<String, Object>> list = (ArrayList<HashMap<String, Object>>) type.get("data");
				HashMap<String, Object> map = JSONParser.introspect(goods);
				Integer goodsId = (Integer) map.get("id");
				if (flag) {
					if (map.get("yestodayOrderedQuantity") == null) {
						map.put("yestodayOrderedQuantity", 0);
					}else{

						for (HashMap<String, Object> map2 : yestodayOrderedQuantity) {
							System.out.println("测试测试");
							if (map2.get("goods_id") == goodsId) {
								map.put("yestodayOrderedQuantity", map2.get("yestoday_quantiy"));
							}
						}	
					}
					if (map.get("yestodaySelledQuantity") == null) {
						map.put("yestodaySelledQuantity", 0);
					}else{
						for (HashMap<String, Object> map3 : yestodayselledQuantity) {
							if (map3.get("goods_id") == goodsId) {
								map.put("yestodaySelledQuantity", map3.get("selled_quantity"));
							}
						}	
					}
					
					
				}

				map.remove("outsourcing");
				map.remove("addtime");
				map.remove("typeName");
				list.add(map);
			}

			return JSONParser.stringify(types);
		} catch (Exception e) {
			e.printStackTrace();
			HashMap<String, Object> map = new HashMap<>();
			map.put("status", "error");
			map.put("code", "100001");
			map.put("msg", "发生错误");
			return JSONParser.stringify(map);
		}
	}

	@Override
	public void updateGoodsStorage(Integer storeId, Integer goodsId, Integer storage) {
		goodsMapper.updateGoodsStorage(storeId, goodsId, storage);
	}

	@Override
	public List<HashMap<String, Object>> findYestodayOrderedQuantityById(Integer storeId) {
		// TODO Auto-generated method stub
		Date d = new Date(System.currentTimeMillis() - 1000 * 60 * 60 * 24);
		String date = DateUtil.formatDate(d);
		return goodsMapper.findYestodayOrderedQuantityById(storeId, date);
	}

	@Override
	public List<HashMap<String, Object>> findYestodaySelledQuantityById(Integer storeId) {
		Date d = new Date(System.currentTimeMillis() - 1000 * 60 * 60 * 24);
		String date = DateUtil.formatDate(d);
		return goodsMapper.findYestodaySelledQuantityById(storeId, date);
	}

	@Override
	public void updateGoodsRefStorage(Integer storeId, Integer goodsId, Integer storage) {
		goodsMapper.updateGoodsRefStorage(storeId, goodsId, storage);
	}

}
